package com.vcms.view.simulation.machine;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.vcms.controller.machine.MachinaryController;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.view.simulation.storemanagement.StoreViewer;
import com.vcms.view.simulation.uicomponent.listener.DoorListener;

@SuppressWarnings("serial")
public class MachinerySimulatorPanel extends Dialog {

    private static final String TITLE = "Machinery Panel";
   // public static final char  CASHDISPLAY = 'a';
   // public static final char  DRINKISPLAY = 'b';

   public static final char        DIALOG = 'a';

    private StoreViewer cashDisplay;
    private StoreViewer drinksDisplay;
    private Checkbox	doorDisplay;
    private MachinaryController  machinaryController;

    public MachinerySimulatorPanel (Frame  fr, MachinaryController machinaryControl) {
        super (fr, TITLE, false);

        machinaryController = machinaryControl;

        Label     lb = new Label(TITLE);
        lb.setFont(new Font ("Helvetica", Font.BOLD, 24));
        lb.setAlignment(Label.CENTER);

        cashDisplay = new StoreViewer(StoreController.CASH, machinaryControl.getMainController().getStoreController());
        drinksDisplay = new StoreViewer(StoreController.DRINK, machinaryControl.getMainController().getStoreController());

        Panel   tp  = new Panel();
        tp.setLayout(new GridLayout(0,1));
        tp.add(cashDisplay);
        tp.add(drinksDisplay);

        Panel   dp = new Panel();
        doorDisplay = new Checkbox();
        doorDisplay.addItemListener(new DoorListener(machinaryController));
        doorDisplay.setLabel("Door Locked");
        dp.add(doorDisplay);

        this.setLayout(new BorderLayout());
        this.add("North", lb);
        this.add("Center", tp);
        this.add("South", dp);
        pack();

        addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
	    	machinaryController.closeMachineryPanel();
            }
	});
    }

    public void display(){
      this.setVisible(true);
    }

    public void closeDown () {
        dispose ();
        //storeCtrl.closeDown();
    }

    public StoreViewer   getCashStoreDisplay(){
      return cashDisplay;
    }

    public StoreViewer  getDrinksStoreDisplay(){
      return drinksDisplay;
    }

     // state = true, door is locked, panel is not active;
     // state = false, door is unlocked, panel is active;
    public void setDoorState(boolean state){
      doorDisplay.setState(state);
      this.setActive(DIALOG, !state);

    }

    public void setActive(char cp, boolean state){
      switch(cp){
        case DIALOG:
          cashDisplay.setActive(state);
          drinksDisplay.setActive(state);
          doorDisplay.setEnabled(state);
          break;
      }
    }

}
